<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Document</title>
</head>

<body>
  <script>
    // 题目1(选做)：const arr=['name','age','hhh','hhh','age','hhh','hhh','hhh']各出现多少次
    // 要求输出一个对象如下：{age: 2,hhh: 5,name: 1}
    const arr = ['name', 'age', 'hhh', 'hhh', 'age', 'hhh', 'hhh', 'hhh']

    function arr1t(abc){
      const obj ={}
      abc.forEach(item => {
      const newArr =  abc.filter(item1=>{
        return item === item1
      })
      obj[item] = newArr.length
      });
      return obj
    }
    console.log(arr1t(arr));

    // 题目2(必做)：现在两个数组，判断在a中的元素，不在b中 const a = [1,2,3,5] const b=[1,3,5,6] 将在a不在b中的筛选出来
    // 要求：输出一个数组：[2]
    const a = [1, 2, 3, 5]
    const b = [1, 3, 5, 6]


    function arr2t(a,b){
      const newArray =[]
     a.forEach(item =>{
      let f =  b.every(item1 => {
          return item !== item1
        })
        if(f)newArray.push(item)
      })
      return newArray
    }
  console.log(arr2t(a,b));
    // 题目3(必做):数组去重(可以用多种方法,至少写一种)
    // 要求：输出一个数组：[1,2,3,9]
    const arr2 = [1, 2, 3, 2, 9]

    // 方法1
    function arrcc1(a){
     for(let i = 0 ;i<a.length;i++){
      for(let j = i+1 ;j<a.length ;j++){
        if(a[i] === a[j]) {
          a.splice(j, 1)
           j--}
     }
    }
    return a
  }
    console.log(arrcc1(arr2));

    // 方法2
    function arrcc2(a){
      const newArray =  []
        a.forEach(item =>{
          if(newArray.indexOf(item) === -1)newArray.push(item)
        })
        return newArray
    }
    console.log(arrcc2(arr2));

    // 方法3

    function arrcc3(a){
      const newArray =  []
      a.forEach(item =>{
      let f =  newArray.every(item1 =>{
            return item1 !== item
          })
          if(f)newArray.push(item)
        })
        return newArray
    }
    console.log(arrcc2(arr2));

    // 方法4

    
  </script>
</body>

</html>